<?php
App::import('Model', 'Potongan');

class PotonganBL extends Potongan {

    public function createPotongan($data = array(), $dataSession = array()) {
        $ret = null;
        if (!empty($data['Payroll']['divisi_id']) && ($data['Potongan']['karyawan_id'])==0) {
            $divisiId = $data['Payroll']['divisi_id'];
            return $this->addByDivisi($data, $divisiId);
        } else if (!empty($data['Payroll']['divisi_id']) && ($data['Potongan']['karyawan_id'])!=0) {
            return $this->addByKaryawan($data);
        }
    }

    public function addByDivisi($data = array(), $divisiId = NULL) {
        $ret = null;
        $arrayKaryawanToInsert = $this->Karyawan->query('SELECT * FROM karyawan WHERE divisi_id = '.$divisiId.' AND id NOT IN (SELECT karyawan_id FROM potongan)');

        foreach ($arrayKaryawanToInsert as $karyawan) {
            //pr($karyawan['karyawan']['id']);
            $data['Potongan']['karyawan_id'] = $karyawan['karyawan']['id'];
//            pr($data);
            $this->create();
            if ($this->save($data)) {
                $ret = true;
            } else {
                $ret = false;
            }
        }

        return $ret;
    }

    public function addByKaryawan($data = array()) {
        $ret = false;

        $this->create();
        if ($this->save($data)) {
            $ret = true;
        }
        
        return $ret;
    }

    public function editPotongan($data = array(), $id = null) {
        $ret = null;
            if ($this->save($data)) {
                $ret = true;
            } else {
                $ret = false;
            }
        return $ret;
    }

    public function readDetail($id = null){
        return $this->read(null, $id);
    }

    public function potonganFind($type='all', $options=array()) {
        return $this->find($type,$options);
    }
}

?>
